import 'dart:math';

import 'package:pacsun/constants/constant.dart';
import 'package:pacsun/utils/PreferenceUtils.dart';
import 'package:pacsun/view/task/theme/app_colors.dart';
import 'package:pacsun/view/task/widgets/cache_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:showcaseview/showcaseview.dart';

final String tempImageSrc =
    'https://aigcblob.blob.core.windows.net/iss-aigc/File/pacsun_202504192148306697.jpeg';

class GuideDialog extends StatelessWidget {
  const GuideDialog({super.key});

  @override
  Widget build(BuildContext context) {
    try {
      ScreenUtil().screenWidth;
    } catch (e) {
      ScreenUtil.init(
        context,
        designSize: const Size(375, 812),
        minTextAdapt: true,
        splitScreenMode: true,
      );
    }
    return Container(
      alignment: Alignment.center,
      width: 310.w,
      height: 200.h,
      padding: EdgeInsets.symmetric(horizontal: 48).r,
      margin: EdgeInsets.only(left: 32.5).r,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/imgs/maskbg1.png'),
          fit: BoxFit.contain,
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(height: 20.h),
          Text('Congratulations! Pacsun Creator',
              style: TextStyle(
                fontSize: 24.sp,
                fontWeight: FontWeight.w600,
                fontFamily: 'Poppins',
              ),
              textAlign: TextAlign.center,
              textScaler: TextScaler.noScaling),
          Container(
            width: 179.w,
            height: 35.h,
            margin: EdgeInsets.only(top: 30).r,
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                  backgroundColor: Color(0xFFD73030),
                  disabledBackgroundColor: Colors.red.shade50,
                  padding: EdgeInsets.zero),
              onPressed: () async {
                bool b = await PreferenceUtils.setBool(
                    Constant.SP_KEY_GUIDE_EXECUTED, true);
                print("数据存储-----$b");
                ShowCaseWidget.of(context).next();
              },
              child: Text('More Rewards',
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w600,
                    fontFamily: 'Poppins',
                    color: Colors.white,
                  ),
                  textScaler: TextScaler.noScaling),
            ),
          ),
          SizedBox(height: 20.h),
          GestureDetector(
            onTap: () {
              ShowCaseWidget.of(context).dismiss();
            },
            child: Text('skip',
                style: customTextStyle(
                  fontFamily: FontFamily.unileverShilling,
                  color: FamilyTextColor.black,
                ),
                textScaler: TextScaler.noScaling),
          )
        ],
      ),
    );
  }
}

class GuideDialog1 extends StatelessWidget {
  const GuideDialog1({super.key});

  static void showOverlayDialog(BuildContext context) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext dialogContext) {
        return Dialog(
          alignment: Alignment.center,
          insetPadding: EdgeInsets.zero,
          surfaceTintColor: Colors.transparent,
          shadowColor: Colors.transparent,
          backgroundColor: Colors.transparent,
          child: GuideDialog2(
            onClose: () {
              Navigator.of(dialogContext).pop();
            },
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    try {
      ScreenUtil().screenWidth;
    } catch (e) {
      ScreenUtil.init(
        context,
        designSize: const Size(375, 812),
        minTextAdapt: true,
        splitScreenMode: true,
      );
    }
    return Container(
      width: 310.w,
      height: 192.h,
      padding: EdgeInsets.symmetric(horizontal: 15).r,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/imgs/maskbg.png'),
          fit: BoxFit.contain,
        ),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          SizedBox(height: 32.h),
          Text('Grab Fashion Samples Now!',
              style: customTextStyle(
                fontSize: 24,
                fontWeight: 600,
                color: FamilyTextColor.black,
                height: 1.3,
              ),
              textAlign: TextAlign.center,
              textScaler: TextScaler.noScaling),
          Container(
            width: 179.w,
            height: 35.h,
            margin: EdgeInsets.only(top: 18).r,
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                  backgroundColor: Color(0xFFD73030),
                  disabledBackgroundColor: Colors.red.shade50,
                  padding: EdgeInsets.zero),
              onPressed: () {
                ShowCaseWidget.of(context).dismiss();
                showOverlayDialog(context);
              },
              child: Text('Next',
                  style: customTextStyle(
                    fontSize: 16,
                    fontWeight: 600,
                  ),
                  textScaler: TextScaler.noScaling),
            ),
          ),
          SizedBox(height: 20.h),
          GestureDetector(
            onTap: () {
              ShowCaseWidget.of(context).dismiss();
            },
            child: Text('skip',
                style: customTextStyle(
                  fontFamily: FontFamily.unileverShilling,
                  color: FamilyTextColor.black,
                ),
                textScaler: TextScaler.noScaling),
          )
        ],
      ),
    );
  }
}

class GuideDialog2 extends StatelessWidget {
  final VoidCallback? onClose;

  const GuideDialog2({
    super.key,
    this.onClose,
  });

  @override
  Widget build(BuildContext context) {
    try {
      ScreenUtil().screenWidth;
    } catch (e) {
      ScreenUtil.init(
        context,
        designSize: const Size(375, 812),
        minTextAdapt: true,
        splitScreenMode: true,
      );
    }

    return Center(
      child: SizedBox(
        width: 310.w,
        height: 230.h,
        child: Stack(
          clipBehavior: Clip.none,
          children: [
            Column(
              children: [
                Container(
                  width: 310.w,
                  height: 192.h,
                  padding: EdgeInsets.symmetric(horizontal: 48).r,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage('assets/imgs/maskbg.png'),
                      fit: BoxFit.contain,
                    ),
                  ),
                  child: Column(
                    children: [
                      SizedBox(height: 50.h),
                      Text(
                        'Earn 100 Points For 1st Post',
                        style: TextStyle(
                          fontSize: 24.sp,
                          fontWeight: FontWeight.w600,
                          fontFamily: 'Poppins',
                        ),
                        textAlign: TextAlign.center,
                        textScaler: TextScaler.noScaling,
                      ),
                      Container(
                        width: 179,
                        height: 35,
                        margin: EdgeInsets.only(top: 18).r,
                        child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                              backgroundColor: Color(0xFFD73030),
                              disabledBackgroundColor: Colors.red.shade50,
                              padding: EdgeInsets.zero),
                          onPressed: () {
                            if (onClose != null) {
                              onClose!();
                            }
                          },
                          child: Text(
                            'Greate A Post',
                            style: TextStyle(
                              fontSize: 16.sp,
                              fontWeight: FontWeight.w600,
                              fontFamily: 'Poppins',
                              color: Colors.white,
                            ),
                            textScaler: TextScaler.noScaling,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                SizedBox(height: 10.h),
                GestureDetector(
                  onTap: () {
                    // ws
                    if (onClose != null) {
                      onClose!();
                    }
                  },
                  child: SvgPicture.asset(
                    'assets/icons/icon_dialog_close.svg',
                    width: 24.w,
                    height: 24.h,
                  ),
                ),
              ],
            ),
            Positioned(
              top: -10.h,
              left: 105.w,
              child: CacheNetworkImage(
                imageUrl: 'assets/imgs/guidemoney.png',
                width: 100.w,
                height: 70.h,
                imageType: ImageType.asset,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
